package com.qinglei.recoup.system.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.domain.RadarData;
import com.qinglei.recoup.common.domain.RadarUserDataFour;
import com.qinglei.recoup.system.dao.FlagBodyMovementMapper;
import com.qinglei.recoup.system.domain.RadarUserData;

import com.qinglei.recoup.system.service.RadarDataService;
import com.qinglei.recoup.system.service.RadarUserDataFourService;
import com.qinglei.recoup.system.service.RadarUserDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * 清理监测仪上传临时数据
 */
@Service
@Slf4j
public class RadarDataTask {

    @Resource
    private RadarUserDataService radarUserDataService;

    @Resource
    private RadarUserDataFourService radarUserDataFourService;

    @Resource
    private FlagBodyMovementMapper flagBodyMovementMapper;

    @Resource
    private RadarDataService radarDataService;

    public void clearOldData() {
        log.debug("clearOldData run");
        List<RadarUserData> snList = radarUserDataService.list(Wrappers.<RadarUserData>lambdaQuery()
                .select(RadarUserData::getSn).groupBy(RadarUserData::getSn));
        snList.forEach(item -> {
            try {
//                RadarUserData one = radarUserDataService.getOne(Wrappers.<RadarUserData>lambdaQuery()
//                        .select(RadarUserData::getSn, RadarUserData::getCreateTime)
//                        .eq(RadarUserData::getSn, item.getSn())
//                        .orderByDesc(RadarUserData::getCreateTime).last("limit 1 offset 20"));
//                if (one != null) {
//                    Date expire = new Date(one.getCreateTime().getTime() - 1);
//                    radarUserDataService.remove(Wrappers.<RadarUserData>lambdaQuery()
//                            .eq(RadarUserData::getSn, item.getSn())
//                            .lt(RadarUserData::getCreateTime, expire));
//                }

                radarUserDataService.remove(Wrappers.<RadarUserData>lambdaQuery());

            }catch (Exception e) {
                log.error("clearOldData sn:{}", item.getSn(), e);
            }
        });
    }


    public void clearOldDataV4() {
        log.debug("clearOldDataV4 run");
        List<RadarUserDataFour> snList = radarUserDataFourService.list(Wrappers.<RadarUserDataFour>lambdaQuery().select(RadarUserDataFour::getSn).groupBy(RadarUserDataFour::getSn));
        snList.forEach(item -> {
            try {
                RadarUserDataFour one = radarUserDataFourService.getOne(Wrappers.<RadarUserDataFour>lambdaQuery()
                        .select(RadarUserDataFour::getSn, RadarUserDataFour::getCreateTime)
                        .eq(RadarUserDataFour::getSn, item.getSn())
                        .orderByDesc(RadarUserDataFour::getCreateTime).last("limit 1 offset 20"));
                if (one != null) {
                    Date expire = new Date(one.getCreateTime().getTime() - 4000);
                    radarUserDataFourService.remove(Wrappers.<RadarUserDataFour>lambdaQuery()
                            .eq(RadarUserDataFour::getSn, item.getSn())
                            .lt(RadarUserDataFour::getCreateTime, expire));
                }
            }catch (Exception e) {
                e.printStackTrace();
                log.error("clearOldData sn:{}", item.getSn(), e);
            }
        });
    }


    public void clearOldDataV7() {
        log.debug("clearOldDataV7 run");
        List<RadarData> snList = radarDataService.list(new QueryWrapper<RadarData>().lambda().select(RadarData::getSn).groupBy(RadarData::getSn));
        snList.forEach(item -> {
            try {
                RadarData radarData = radarDataService.getOne(new QueryWrapper<RadarData>().lambda()
                        .select(RadarData::getSn, RadarData::getCreateTime)
                        .eq(RadarData::getSn, item.getSn())
                        .orderByDesc(RadarData::getCreateTime).last("limit 1"));
                if (radarData != null) {
                    Date expire = new Date(radarData.getCreateTime().getTime() - 3000);
                    radarDataService.remove(new QueryWrapper<RadarData>().lambda()
                            .eq(RadarData::getSn, item.getSn())
                            .lt(RadarData::getCreateTime, expire));
                }
            }catch (Exception e) {
                e.printStackTrace();
                log.error("clearOldData sn:{}", item.getSn(), e);
            }
        });
    }



    public void clearFlagBodyMovement(){
        QueryWrapper queryWrapperDel = new QueryWrapper();
        queryWrapperDel.lt("create_time", LocalDate.now()+" " +  LocalTime.now().minusMinutes(11));
        flagBodyMovementMapper.delete(queryWrapperDel);

    }
}
